package com.yndsoft.jc.supplier.net;

import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.supplier.net.util.RetrofitUtil;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class HttpUtil {

    private static HttpUtil httpUtil = new HttpUtil();
    private ApiService apiService;

    private HttpUtil() {
        apiService = RetrofitUtil.getInstance().getApiService();
    }

    public static HttpUtil getInstance() {
        return httpUtil;
    }

    private <T> void toSubscribe(Subscriber<T> subscriber, Observable<T> observable) {
        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    /**
     * 以下自定义提交/获取服务器数据
     */

    public void put(Subscriber<ReturnMsg> subscriber, String type, String json) {
        toSubscribe(subscriber, apiService.put(type, json));
    }

    public void verifyCode(Subscriber<ReturnMsg> subscriber, String type, String phone, int otype) {
        toSubscribe(subscriber, apiService.verifyCode(type, phone, otype));
    }

    public void uploadImg(Subscriber<ReturnMsg> subscriber, String pic_data) {
        toSubscribe(subscriber, apiService.uploadImg(pic_data));
    }

    public void supplierInfo(Subscriber<ReturnMsg> subscriber, String type, String sid) {
        toSubscribe(subscriber, apiService.supplierInfo(type, sid));
    }

    public void supplierOrder(Subscriber<ReturnMsg> subscriber, String type, String sid, int tid, int index, int size) {
        toSubscribe(subscriber, apiService.supplierOrder(type, sid, tid, index, size));
    }

    public void otherOrder(Subscriber<ReturnMsg> subscriber, String type, String sid, int index, int size) {
        toSubscribe(subscriber, apiService.otherOrder(type, sid, index, size));
    }

    public void supplierOrderDetail(Subscriber<ReturnMsg> subscriber, String type, String sid, String id) {
        toSubscribe(subscriber, apiService.supplierOrderDetail(type, sid, id));
    }

    public void orderWait(Subscriber<ReturnMsg> subscriber, String type, String id, String sid) {
        toSubscribe(subscriber, apiService.orderWait(type, id, sid));
    }

    public void supplierComment(Subscriber<ReturnMsg> subscriber, String type, String sid, int index, int size) {
        toSubscribe(subscriber, apiService.supplierComment(type, sid, index, size));
    }

    public void supplierHoliday(Subscriber<ReturnMsg> subscriber, String type, String sid) {
        toSubscribe(subscriber, apiService.supplierHoliday(type, sid));
    }

    public void supplierEntry(Subscriber<ReturnMsg> subscriber, String type, String sid, int tid) {
        toSubscribe(subscriber, apiService.supplierEntry(type, sid, tid));
    }

    public void province(Subscriber<ReturnMsg> subscriber, String type) {
        toSubscribe(subscriber, apiService.province(type));
    }

    public void city(Subscriber<ReturnMsg> subscriber, String type, int pid) {
        toSubscribe(subscriber, apiService.city(type, pid));
    }

    public void perfectList(Subscriber<ReturnMsg> subscriber, String type, String sid, int index, int size) {
        toSubscribe(subscriber, apiService.perfectList(type, sid, index, size));
    }

    public void perfectDelete(Subscriber<ReturnMsg> subscriber, String type, String id, String sid) {
        toSubscribe(subscriber, apiService.perfectDelete(type, id, sid));
    }

    public void category(Subscriber<ReturnMsg> subscriber, String type) {
        toSubscribe(subscriber, apiService.category(type));
    }

    public void product(Subscriber<ReturnMsg> subscriber, String type, String pid, int index, int size) {
        toSubscribe(subscriber, apiService.product(type, pid, index, size));
    }

    public void productAttribute(Subscriber<ReturnMsg> subscriber, String type, String id) {
        toSubscribe(subscriber, apiService.productAttribute(type, id));
    }

    public void hrDelete(Subscriber<ReturnMsg> subscriber, String type, String id, String sid) {
        toSubscribe(subscriber, apiService.hrDelete(type, id, sid));
    }

    public void entrySignOrder(Subscriber<ReturnMsg> subscriber, String type, String id, String sid, String json) {
        toSubscribe(subscriber, apiService.entrySignOrder(type, id, sid, json));
    }

    public void entryDetail(Subscriber<ReturnMsg> subscriber, String type, String id, String odetid, String sid) {
        toSubscribe(subscriber, apiService.entryDetail(type, id, odetid, sid));
    }
}